<?php
require_once '../Model/DBconnect.php';

if(isset($_GET['filter'])){
	if($_GET['filter'] == "AllPlayers"){
		echo "<tr>
					<th>Pos</th>
					<th><a class='ismPositionLink'>All Players</a></th>
					<th>Team</th>
					<th>$</th>
					<th><abbr title='Total score'>Score</abbr></th>
				</tr>";
		include "../Controller/OrderPlayersController.php";
	}
	else{
		$dbconnect = DBconnect::getInstance();
		$query = $dbconnect->selectAllPlayersFromTeam($_GET['filter']);
		if(mysql_num_rows($query) > 0){
			$team_name = mysql_result($query, 0,'team_name');
			echo "<tr>
					<th>Pos</th>
					<th colspan='2'><a class='ismPositionLink'>$team_name</a></th>
					<th>$</th>
					<th><abbr title='Total score'>Score</abbr></th>
				</tr>";
			while($players = mysql_fetch_assoc($query)){
				$position_name = $players['pos_name'];
				$short_pos_name = strtoupper(substr($position_name, 0, 3));
				$player_name = $players['player_name'];
				$team_short_name = $players['short_name'];
				$value = $players['value'];
				$total_score = $players['total_score'];
				$position_id = $players['position_id'];
				settype($position_id, "int");
				$player_no = $players['player_no'];
				settype($player_no, "int");
				$setFunctionName = "set".$position_name."(".$player_no.", ".$value.")";		
				echo "<tr onclick='$setFunctionName'><td id='shortName'>$short_pos_name</td><td id='playerNameTD'>$player_name</td>
													 <td id='shortName'>$team_short_name</td><td id='values'>$value</td><td id='values'>$total_score</td></tr>";
			}
		}
		else {
			$query = $dbconnect->selectAllPlayersFromPosition($_GET['filter']);
			echo "<tr>
					<th colspan='2'><a class='ismPositionLink'>".$_GET['filter']."s</a></th>
					<th>Team</th>
					<th>$</th>
					<th><abbr title='Total score'>Score</abbr></th>
				</tr>";
			while($players = mysql_fetch_assoc($query)){
				$position_name = $players['pos_name'];
				$player_name = $players['player_name'];
				$team_short_name = $players['short_name'];
				$value = $players['value'];
				$total_score = $players['total_score'];
				$position_id = $players['id'];
				settype($position_id, "int");
				$player_no = $players['player_no'];
				settype($player_no, "int");
				$short_pos_name = strtoupper(substr($position_name, 0, 3));
				$setFunctionName = "set".$position_name."(".$player_no.", ".$value.")";
				echo "<tr onclick='$setFunctionName'><td id='shortName'>$short_pos_name</td><td id='playerNameTD'>$player_name</td>
				<td id='shortName'>$team_short_name</td><td id='values'>$value</td><td id='values'>$total_score</td></tr>";
			}
		}
	}
}

?>